package com.chenglu.android.common.base;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.lifecycle.Lifecycle;


import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chenglu.android.R;
import com.chenglu.android.common.widget.LoadingDialog;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import autodispose2.AutoDispose;
import autodispose2.AutoDisposeConverter;
import autodispose2.androidx.lifecycle.AndroidLifecycleScopeProvider;

/**
 * @author lyj
 * @Created by Copyright © 2023 Guangzhou Hongma Technology Co., Ltd. All rights reserved.
 * @Description  MVPfragment基类
 */
public abstract class BaseMvpFragment<T extends BasePresenter> extends BaseFragment implements BaseView {

    protected T mPresenter;

    private LoadingDialog loadingDialog;

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mPresenter != null) {
            mPresenter.detachView();
        }
        super.onDestroyView();
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        loadingDialog = new LoadingDialog(mContext);
        loadingDialog.setCancelable(loadingCancelable());
        //创建present

        if (mPresenter != null) {
//            mPresenter = createPresenter();
            mPresenter.attachView(this);

        }

    }

    /**
     * 返回presenter
     *
     * @return P
     */
    protected T getPresenter() {
        return mPresenter;
    }
//
//    protected abstract T createPresenter();


    //***************************************IBaseView方法实现 start *************************************


    @Override
    public void showLoading() {
        if (loadingDialog != null && !loadingDialog.isShowing()) {
            loadingDialog.show();
        }
//        hideEmptyView();
    }

    @Override
    public void dismissLoading() {
        if (loadingDialog != null && loadingDialog.isShowing()) {
            loadingDialog.dismiss();
        }
    }

    /**
     * 加载框是否可以取消
     *
     * @return boolean
     */
    protected boolean loadingCancelable() {
        return true;
    }

    @Override
    public void onEmpty(Object tag) {
        dismissLoading();
    }

    @Override
    public void onError(String errMessage) {

    }

    @Override
    public void onNetWorkError(Object tag) {

    }

    @Override
    public void onNetOutTimeError(String tag) {

    }

    /**
     * 停止刷新和加载更多
     *
     * @param layout 刷新layout
     */
    protected void stopRefresh(SmartRefreshLayout layout) {
        if (layout == null) {
            return;
        }
        layout.finishLoadMore();
        layout.finishRefresh();
    }


    /**
     *
     * @description 停止上拉刷新 下来加载更多数据
     *
     */
    protected  void  stopLoadMoreRefresh(BaseQuickAdapter baseQuickAdapter ,SmartRefreshLayout layout, int total, int current, int size
    ,boolean isAddFooterView){
        if(layout ==null){
            return;
        }
        if (total <size) {
            layout.setEnableRefresh(false);
        }else {
            layout.setEnableRefresh(true);
        }
        if (current * size >= total) {
            if(isAddFooterView){
                View inflate = Global.inflate(R.layout.loadmore_addfooter_layout);
                inflate.findViewById(R.id.tv_no_more).setBackgroundColor(getResources().getColor(R.color.color_f6f6f6));
                baseQuickAdapter.addFooterView(inflate);

            }

        } else {

            baseQuickAdapter.removeAllFooterView();
            layout.setEnableLoadMore(true);
        }

    }

    //***************************************IBaseView方法实现 end*************************************
    /**
     * 绑定生命周期 防止MVP内存泄漏
     *
     * @param <T>
     * @return
     */
    @Override
    public <T> AutoDisposeConverter<T> bindAutoDispose() {
        return AutoDispose.autoDisposable(AndroidLifecycleScopeProvider
                .from(this, Lifecycle.Event.ON_DESTROY));
    }
}
